package api

import (
	"net/http"

	"example.com/example/dao"
	"example.com/example/model"

	"github.com/gin-gonic/gin"
	"github.com/guregu/null"
	"github.com/julienschmidt/httprouter"
)

var (
	_ = null.Bool{}
)

func configCustomProcessBaseRouter(router *httprouter.Router) {
	router.GET("/customprocessbase", GetAllCustomProcessBase)
	router.POST("/customprocessbase", AddCustomProcessBase)
	router.GET("/customprocessbase/:argId", GetCustomProcessBase)
	router.PUT("/customprocessbase/:argId", UpdateCustomProcessBase)
	router.DELETE("/customprocessbase/:argId", DeleteCustomProcessBase)
}

func configGinCustomProcessBaseRouter(router gin.IRoutes) {
	router.GET("/customprocessbase", ConverHttprouterToGin(GetAllCustomProcessBase))
	router.POST("/customprocessbase", ConverHttprouterToGin(AddCustomProcessBase))
	router.GET("/customprocessbase/:argId", ConverHttprouterToGin(GetCustomProcessBase))
	router.PUT("/customprocessbase/:argId", ConverHttprouterToGin(UpdateCustomProcessBase))
	router.DELETE("/customprocessbase/:argId", ConverHttprouterToGin(DeleteCustomProcessBase))
}

// GetAllCustomProcessBase is a function to get a slice of record(s) from custom_process_base table in the custom_process database
// @Summary Get list of CustomProcessBase
// @Tags CustomProcessBase
// @Description GetAllCustomProcessBase is a handler to get a slice of record(s) from custom_process_base table in the custom_process database
// @Accept  json
// @Produce  json
// @Param   page     query    int     false        "page requested (defaults to 0)"
// @Param   pagesize query    int     false        "number of records in a page  (defaults to 20)"
// @Param   order    query    string  false        "db sort order column"
// @Success 200 {object} api.PagedResults{data=[]model.CustomProcessBase}
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /customprocessbase [get]
// http "http://localhost:8080/customprocessbase?page=0&pagesize=20" X-Api-User:user123
func GetAllCustomProcessBase(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	page, err := readInt(r, "page", 0)
	if err != nil || page < 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	pagesize, err := readInt(r, "pagesize", 20)
	if err != nil || pagesize <= 0 {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	order := r.FormValue("order")

	if err := ValidateRequest(ctx, r, "custom_process_base", model.RetrieveMany); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	records, totalRows, err := dao.GetAllCustomProcessBase(ctx, page, pagesize, order)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	result := &PagedResults{Page: page, PageSize: pagesize, Data: records, TotalRecords: totalRows}
	writeJSON(ctx, w, result)
}

// GetCustomProcessBase is a function to get a single record from the custom_process_base table in the custom_process database
// @Summary Get record from table CustomProcessBase by  argId
// @Tags CustomProcessBase
// @ID argId
// @Description GetCustomProcessBase is a function to get a single record from the custom_process_base table in the custom_process database
// @Accept  json
// @Produce  json
// @Param  argId path int64 true "id"
// @Success 200 {object} model.CustomProcessBase
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError "ErrNotFound, db record for id not found - returns NotFound HTTP 404 not found error"
// @Router /customprocessbase/{argId} [get]
// http "http://localhost:8080/customprocessbase/1" X-Api-User:user123
func GetCustomProcessBase(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argId, err := parseUint64(ps, "argId")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base", model.RetrieveOne); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	record, err := dao.GetCustomProcessBase(ctx, argId)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, record)
}

// AddCustomProcessBase add to add a single record to custom_process_base table in the custom_process database
// @Summary Add an record to custom_process_base table
// @Description add to add a single record to custom_process_base table in the custom_process database
// @Tags CustomProcessBase
// @Accept  json
// @Produce  json
// @Param CustomProcessBase body model.CustomProcessBase true "Add CustomProcessBase"
// @Success 200 {object} model.CustomProcessBase
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /customprocessbase [post]
// echo '{"from_url": "jesJyZxwbnYDLCybnRgijhcDV","to_url": "FXNmqAcvwwCEOrKleInNmHgqI","id": 14,"name": "dHTlcLJQEqjFvJKeBNcPRBueo","create_time": "2178-09-15T16:07:40.493479713+08:00","update_time": "2067-11-28T23:49:21.104781313+08:00"}' | http POST "http://localhost:8080/customprocessbase" X-Api-User:user123
func AddCustomProcessBase(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)
	customprocessbase := &model.CustomProcessBase{}

	if err := readJSON(r, customprocessbase); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := customprocessbase.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	customprocessbase.Prepare()

	if err := customprocessbase.Validate(model.Create); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base", model.Create); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	var err error
	customprocessbase, _, err = dao.AddCustomProcessBase(ctx, customprocessbase)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, customprocessbase)
}

// UpdateCustomProcessBase Update a single record from custom_process_base table in the custom_process database
// @Summary Update an record in table custom_process_base
// @Description Update a single record from custom_process_base table in the custom_process database
// @Tags CustomProcessBase
// @Accept  json
// @Produce  json
// @Param  argId path int64 true "id"
// @Param  CustomProcessBase body model.CustomProcessBase true "Update CustomProcessBase record"
// @Success 200 {object} model.CustomProcessBase
// @Failure 400 {object} api.HTTPError
// @Failure 404 {object} api.HTTPError
// @Router /customprocessbase/{argId} [put]
// echo '{"from_url": "jesJyZxwbnYDLCybnRgijhcDV","to_url": "FXNmqAcvwwCEOrKleInNmHgqI","id": 14,"name": "dHTlcLJQEqjFvJKeBNcPRBueo","create_time": "2178-09-15T16:07:40.493479713+08:00","update_time": "2067-11-28T23:49:21.104781313+08:00"}' | http PUT "http://localhost:8080/customprocessbase/1"  X-Api-User:user123
func UpdateCustomProcessBase(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argId, err := parseUint64(ps, "argId")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	customprocessbase := &model.CustomProcessBase{}
	if err := readJSON(r, customprocessbase); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := customprocessbase.BeforeSave(); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
	}

	customprocessbase.Prepare()

	if err := customprocessbase.Validate(model.Update); err != nil {
		returnError(ctx, w, r, dao.ErrBadParams)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base", model.Update); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	customprocessbase, _, err = dao.UpdateCustomProcessBase(ctx,
		argId,
		customprocessbase)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeJSON(ctx, w, customprocessbase)
}

// DeleteCustomProcessBase Delete a single record from custom_process_base table in the custom_process database
// @Summary Delete a record from custom_process_base
// @Description Delete a single record from custom_process_base table in the custom_process database
// @Tags CustomProcessBase
// @Accept  json
// @Produce  json
// @Param  argId path int64 true "id"
// @Success 204 {object} model.CustomProcessBase
// @Failure 400 {object} api.HTTPError
// @Failure 500 {object} api.HTTPError
// @Router /customprocessbase/{argId} [delete]
// http DELETE "http://localhost:8080/customprocessbase/1" X-Api-User:user123
func DeleteCustomProcessBase(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	ctx := initializeContext(r)

	argId, err := parseUint64(ps, "argId")
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	if err := ValidateRequest(ctx, r, "custom_process_base", model.Delete); err != nil {
		returnError(ctx, w, r, err)
		return
	}

	rowsAffected, err := dao.DeleteCustomProcessBase(ctx, argId)
	if err != nil {
		returnError(ctx, w, r, err)
		return
	}

	writeRowsAffected(w, rowsAffected)
}
